package com.lc.zpyh.ui.activity.mine;

import android.os.Bundle;
import android.view.View;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.widget.view.ClearEditText;
import com.lc.widget.view.CountdownView;
import com.lc.widget.view.RegexEditText;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.ResetPasswordApi;
import com.lc.zpyh.http.request.SendSmsApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ForgetPassWordActivity extends AppActivity {
    @BindView(R.id.et_phone)
    ClearEditText etPhone;
    @BindView(R.id.et_login_code)
    RegexEditText etLoginCode;
    @BindView(R.id.et_password)
    ClearEditText etPassword;
    @BindView(R.id.et_password_agine)
    ClearEditText etPasswordAgine;
    @BindView(R.id.cv_phone_reset_countdown)
    CountdownView cvPhoneResetCountdown;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forgetpassword;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        forget();
    }

    private void forget() {
    }

    @OnClick({R.id.cv_phone_reset_countdown, R.id.btn_phone_reset_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cv_phone_reset_countdown:
                if (etPhone.getText().toString().length() != 11) {
                    toast(R.string.common_phone_input_error);
                    return;
                }

                EasyHttp.post(this)
                        .api(new SendSmsApi()
                                .setPhone(etPhone.getText().toString())).request(new HttpCallback<PublicMsgBean>(this) {

                    @Override
                    public void onStart(Call call) {
                        cvPhoneResetCountdown.start();
                    }

                    @Override
                    public void onSucceed(PublicMsgBean data) {
                        toast(data.getList());
                    }


                });
                break;
            case R.id.btn_phone_reset_commit:
                if (etPhone.getText().toString().length() != 11) {
                    toast(R.string.common_phone_input_error);
                    return;
                }
                if (etLoginCode.getText().toString().isEmpty()) {
                    toast(R.string.common_code_input_hint);
                    return;
                }
                if (etPassword.getText().toString().isEmpty()) {

                    toast(R.string.common_password_input_error);
                    return;
                }
                if (etPasswordAgine.getText().toString().isEmpty()) {

                    toast(R.string.common_password_input_error);
                    return;
                }
                if (!etPassword.getText().toString().trim().equals(etPasswordAgine.getText().toString().trim())){
                    toast(R.string.common_password_input_unlike);
                    return;
                }
                EasyHttp.post(this)
                        .api(new ResetPasswordApi()
                                .setUserPhone(etPhone.getText().toString()).setCode(etLoginCode.getText().toString().trim()).setNewPassWord(etPasswordAgine.getText().toString().trim())).request(new HttpCallback<PublicMsgBean>(this) {



                    @Override
                    public void onSucceed(PublicMsgBean data) {
                        toast(data.getList());
                        finish();
                    }


                });
                break;
        }
    }
}
